package com.volkanic.model.buttons;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;

/**
 * Created by Lucas on 20/07/2014.
 */
public class MotionTextButton extends TextButton {

    /**
     * The final position of the textButton
     */
    private Vector2 finalPosition;
    /**
     * The speed of the moving BackgroundImage
     */
    private float speed;
    /**
     * The ration of decreasing speed
     */
    private final float decreaseSpeed = 1f/25f;
    /**
     * Determine if the motionButton is moving
     */
    private boolean _isMoving;
    /**
     * Create a button with motion
     * @param text The text to insert inside the button
     * @param style The style of the button
     * @param originPosition the point of origin
     * @param endPosition the final point
     */
    public MotionTextButton(String text, TextButtonStyle style, Vector2 originPosition, Vector2 endPosition, float speed, float width) {
        super(text, style);

        this.setWidth(width);

        this.finalPosition = endPosition;
        this.setCenterPosition(originPosition.x, originPosition.y);
        this.speed = speed;
        _isMoving = true;
    }

    @Override
    public void act(float delta) {
        super.act(delta);

        if(_isMoving) {
            // We check if the button need to go further down
            if (this.getCenterY() > finalPosition.y) {
                this.moveYTo(this.getNextCenterYCoordinate());
            } else {
                // if the button reach its final position it doesn't have to move anymore
                _isMoving = false;
            }
        }
    }

    /**
     * Move the background on the Y coordinate
     * Decrease the speed
     * @param newY the new center Y coordinate
     */
    private void moveYTo(float newY){
        this.setCenterPosition(this.getCenterX(), newY);
        // Decrease the speed of the Image
        if (speed - decreaseSpeed >= 1f) {
            speed -= decreaseSpeed;
        }
    }

    /**
     * Check the next position of the Y coordinate.
     * If the next Y coordinate goes too far, recalibrate the next Y coordinate
     * @return the next correct Y coordinate
     */
    private float getNextCenterYCoordinate(){
        if (this.getCenterY() - speed <= finalPosition.y) {
            return finalPosition.y;
        }

        return this.getCenterY() - speed;
    }
}
